<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>取得与指定的颜色加透明度最接近的颜色</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.imagecolorclosest.html">« imagecolorclosest</a></li>
      <li style="float: right;"><a href="function.imagecolorclosesthwb.html">imagecolorclosesthwb »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.image.html">GD 和图像处理 函数</a></li>
    <li>取得与指定的颜色加透明度最接近的颜色</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.imagecolorclosestalpha" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">imagecolorclosestalpha</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">imagecolorclosestalpha</span> &mdash; <span class="dc-title">取得与指定的颜色加透明度最接近的颜色</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.imagecolorclosestalpha-description">
  <h3 class="title">说明</h3>
 <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>imagecolorclosestalpha</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">resource</span> <code class="parameter">$image</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$red</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$green</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$blue</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$alpha</code></span><br>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   返回图像调色板中与指定的 <abbr title="Red-Green-Blue">RGB</abbr> 值以及
   <code class="parameter">alpha</code> 深度最“接近”的颜色。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.imagecolorclosestalpha-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    <dt>

<code class="parameter">image</code></dt>
<dd>
<p class="para">由图象创建函数(例如<span class="function"><a href="function.imagecreatetruecolor.html" class="function">imagecreatetruecolor()</a></span>)返回的 
<span class="classname"><a href="class.gdimage.html" class="classname">GdImage</a></span> 对象。</p></dd>

    
     <dt>
<code class="parameter">red</code></dt>

     <dd>

      <p class="para">红色成分的值。</p>
     </dd>

    
    
     <dt>
<code class="parameter">green</code></dt>

     <dd>

      <p class="para">绿色成分的值。</p>
     </dd>

    
    
     <dt>
<code class="parameter">blue</code></dt>

     <dd>

      <p class="para">蓝色成分的值。</p>
     </dd>

    
    
     <dt>
<code class="parameter">alpha</code></dt>

     <dd>

      <p class="para">
       A value between <code class="literal">0</code> and <code class="literal">127</code>.
       <code class="literal">0</code> indicates completely opaque while 
       <code class="literal">127</code> indicates completely transparent.
      </p>
     </dd>

    
   </dl>

   The colors parameters are integers between 0 and 255 or hexadecimals
   between 0x00 and 0xFF.
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.imagecolorclosestalpha-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the index of the closest color in the palette.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.imagecolorclosestalpha-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2636">
    <p><strong>示例 #1 Search for a set of colors in an image</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Start&nbsp;with&nbsp;an&nbsp;image&nbsp;and&nbsp;convert&nbsp;it&nbsp;to&nbsp;a&nbsp;palette-based&nbsp;image<br /></span><span style="color: #0000BB">$im&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecreatefrompng</span><span style="color: #007700">(</span><span style="color: #DD0000">'figures/imagecolorclosest.png'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagetruecolortopalette</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">255</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Search&nbsp;colors&nbsp;(RGB)<br /></span><span style="color: #0000BB">$colors&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">254</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">145</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">154</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">50</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">153</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">145</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">188</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">127</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">153</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">90</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">145</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">255</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">137</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">92</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">84</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #FF8000">//&nbsp;Loop&nbsp;through&nbsp;each&nbsp;search&nbsp;and&nbsp;find&nbsp;the&nbsp;closest&nbsp;color&nbsp;in&nbsp;the&nbsp;palette.<br />//&nbsp;Return&nbsp;the&nbsp;search&nbsp;number,&nbsp;the&nbsp;search&nbsp;RGB&nbsp;and&nbsp;the&nbsp;converted&nbsp;RGB&nbsp;match<br /></span><span style="color: #007700">foreach(</span><span style="color: #0000BB">$colors&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$rgb</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorclosestalpha</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$rgb</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$rgb</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$rgb</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$rgb</span><span style="color: #007700">[</span><span style="color: #0000BB">3</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorsforindex</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"(</span><span style="color: #007700">{</span><span style="color: #0000BB">$result</span><span style="color: #007700">[</span><span style="color: #DD0000">'red'</span><span style="color: #007700">]}</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$result</span><span style="color: #007700">[</span><span style="color: #DD0000">'green'</span><span style="color: #007700">]}</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$result</span><span style="color: #007700">[</span><span style="color: #DD0000">'blue'</span><span style="color: #007700">]}</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$result</span><span style="color: #007700">[</span><span style="color: #DD0000">'alpha'</span><span style="color: #007700">]}</span><span style="color: #DD0000">)"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"#</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">:&nbsp;Search&nbsp;(</span><span style="color: #0000BB">$rgb</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #0000BB">$rgb</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #0000BB">$rgb</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #0000BB">$rgb</span><span style="color: #007700">[</span><span style="color: #0000BB">3</span><span style="color: #007700">]</span><span style="color: #DD0000">);&nbsp;Closest&nbsp;match:&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">imagedestroy</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
#0: Search (254, 145, 154, 50); Closest match: (252, 150, 148, 0).
#1: Search (153, 145, 188, 127); Closest match: (148, 150, 196, 0).
#2: Search (153, 90, 145, 0); Closest match: (148, 90, 156, 0).
#3: Search (255, 137, 92, 84); Closest match: (252, 150, 92, 0).
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.imagecolorclosestalpha-notes">
  <h3 class="title">注释</h3>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.imagecolorclosestalpha-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.imagecolorexactalpha.html" class="function" rel="rdfs-seeAlso">imagecolorexactalpha()</a> - 取得指定的颜色加透明度的索引值</span></li>
    <li class="member"><span class="function"><a href="function.imagecolorclosest.html" class="function" rel="rdfs-seeAlso">imagecolorclosest()</a> - 取得与指定的颜色最接近的颜色的索引值</span></li>
    <li class="member"><span class="function"><a href="function.imagecolorclosesthwb.html" class="function" rel="rdfs-seeAlso">imagecolorclosesthwb()</a> - 取得与给定颜色最接近的色度的黑白色的索引</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>